const app = getApp();

Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    theme: app.globalData.theme, //全局颜色变量
    day: 7,
    dayList: [],
    id: 0,
    timeIndex: null,
    timeArr: [{
      "time": "00:00-01:59",
      "freightPrice": 20,
      // 0 不可选，1 可选
      "enable": "0"
    }],
    loading: false,
    selectDay: null,
    selectTime: null
  },
  onLoad: function (options) {
    this.loadServiceTimeList()
    this.start()
  },
  // 加载服务时间
  loadServiceTimeList() {
    let city = app.globalData.addressComponent.city
    if (city) {
      app.api.serviceTimeList({
        serviceCity: city
      }).then(res => {
        this.setData({
          timeArr: res.data || []
        })
      })
    }
  },
  start() {
    let that = this
    let dayList = []
    let base = new Date().getDay()
    for (let i = 0; i < that.data.day; i++) {
      let week = i + base
      week = week > 6 ? week % 7 : week
      dayList.push({
        day: that.getDay(i),
        week: i === 0 ? '今天' : (i === 1 ? '明天' : that.week(week))
      })
    }
    that.setData({
      dayList: dayList,
      selectDay: dayList[0].day
    })
  },
  week(i) {
    let week
    week = " 周" + "日一二三四五六".charAt(i)
    return week
  },
  getDay(day) {
    let today = new Date()
    let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
    today.setTime(targetday_milliseconds)
    let tYear = today.getFullYear()
    let tMonth = today.getMonth()
    let tDate = today.getDate()
    tMonth = this.doHandleMonth(tMonth + 1)
    tDate = this.doHandleMonth(tDate)
    return tMonth + "月" + tDate + "日"
  },
  doHandleMonth(month) {
    var m = month
    if (month.toString().length === 1) {
      m = "0" + month
    }
    return m
  },
  // 选择日期
  getId(e) {
    this.setData({
      id: e.currentTarget.dataset.id,
      selectDay: e.currentTarget.dataset.day
    })
  },
  // 选择时间
  selectTime(e) {
    this.setData({
      timeIndex: e.currentTarget.dataset.index,
      selectTime: e.currentTarget.dataset.time
    })
  },
  timeSub() {
    // this.setData({
    //   loading: true
    // })
    if (this.data.selectDay && this.data.selectTime) {
      app.globalData.bookingTime = this.data.selectDay + " " + this.data.selectTime
    }
    wx.navigateBack()
  }
});